﻿using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;
using TodoList.ArdalisEndpoints.DTOs;
using TodoList.ArdalisEndpoints.Mapper;
using TodoList.Data;

namespace TodoList.ArdalisEndpoints.Endpoints.TodoList;

public class Create(ApplicationDbContext db) : EndpointBaseAsync
	.WithRequest<CreateTodoItemRequest>
	.WithActionResult
{
    [HttpPost("api/[namespace]")]
	public override async Task<ActionResult> HandleAsync([FromBody]CreateTodoItemRequest request, CancellationToken cancellationToken = default)
	{
		var item = request.ToEntity();
		db.TodoItems.Add(item);
		await db.SaveChangesAsync(cancellationToken);
		return Ok("Todo item created successfully.");
	}
}
